package com.github.calvin.view.sortListView;


import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.github.calvin.R;
import com.github.library.sortListView.SideBar;
import com.github.library.sortListView.SortAdapter;
import com.github.library.sortListView.entity.SortModel;

import java.util.ArrayList;
import java.util.List;

public class SortListActivity extends Activity {
    private ListView sortListView;
    private SideBar sideBar;

    private TextView dialog;
    private SortAdapter adapter;
    private List<SortModel> SourceDateList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sort_list);
        initView();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_sort_list, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private void initView() {
        sideBar = (SideBar) findViewById(R.id.sidrbar);
        dialog = (TextView) findViewById(R.id.dialog);
        sideBar.setTextView(dialog);
        sideBar.setOnTouchingLetterChangedListener(new SideBar.OnTouchingLetterChangedListener() {
            @Override
            public void onTouchingLetterChanged(String s) {
                int position = adapter.getPositionForSection(s.charAt(0));
                if (position != -1) {
                    sortListView.setSelection(position);
                }
            }
        });

        sortListView = (ListView) findViewById(R.id.sortlist);
        sortListView.setBackgroundColor(Color.GRAY);
        sortListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Toast.makeText(getApplication(), ((SortModel) adapter.getItem(position)).getName(), Toast.LENGTH_SHORT).show();
            }
        });

        SourceDateList = fillData(getInitData());
        adapter = new SortAdapter(this, SourceDateList);
        sortListView.setAdapter(adapter);
    }

    private List<SortModel> fillData(String[] data) {
        List<SortModel> sortModelList = new ArrayList<>();
        for (int i = 0; i < data.length; i++) {
            SortModel sortModel = new SortModel();
            String sortString = data[i].trim().substring(0, 1).toUpperCase();
            if (sortString.matches("[A-Z]")) {
                sortModel.setSortLetters(sortString.toUpperCase());
            } else {
                sortModel.setSortLetters("#");
            }

            sortModelList.add(sortModel);
        }

        return sortModelList;
    }

    private String[] getInitData() {
        String[] data = {"setOnItemClickListener", "AdapterView", "getItem", "fgD", "swE", "wdfF", "G", "H", "I",
                "sdwJ", "asqK", "Lsdjsd", "M", "makeText", "O", "P", "OnItemClickListener", "R", "S", "T", "U", "V",
                "W", "X", "Ysdwwd", "Z", "#"};

        return data;
    }

}
